package top.cmx.learn.thread;

/**
 * @author cmx
 */
public class Thread2 implements Runnable {

    private int tickets = 100;

    @Override
    public void run() {

        while (true) {
            sellTickets();
        }
    }
    //synchronized 枷锁 方法 代码块
    //死锁 1.系统资源有限 2多个线程执行顺序不合理

    private synchronized void sellTickets() {
        if (tickets > 0) {
            System.out.println(Thread.currentThread().getName() + tickets);
            tickets--;
        }
    }

    public static void main(String[] args) {
        Thread2 thread22 = new Thread2();
        Thread thread2 = new Thread(thread22);
        Thread thread21 = new Thread(thread22);
        thread2.setName("t1 售票点：");
        thread21.setName("t2 售票点：");
        thread2.start();
        thread21.start();
    }


}
